home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / PASCAL / 2009.ZIP / STRLINK.ZIP / OBJECTA.PAS < prev    next >
Pascal/Delphi Source File  |  1989-08-17  |  6KB  |  184 lines

  1. {$D+,R-,S-}
  2. UNIT ObjectA;
  3.  
  4. {
  5.             Rob Rosenberger        CIS: 74017,1344
  6.             Barn Owl Software      VOX: (618) 632-7345
  7.             P.O. Box #74           BBS: (618) 398-5703
  8.             O'Fallon, IL  62269    HST: (618) 398-2305
  9.  
  10.    This Turbo Pascal 5.5 unit builds on the OBJECTS.PAS file on disk three
  11. of the TP package.  The Objects unit provides some excellent linked-list
  12. capabilities, but it doesn't allow arbitrary placement of a node on the list.
  13. This unit offers a decendent, LinkList, which provides two extra methods:
  14. LinkList.Before, to place a node just before some other node in the list, and
  15. LinkList.After, to place a node just after some other node in the list.
  16.    LinkList.Init and LinkList.Done are included because the TP5.5 OOP guide
  17. recommends these common procedures.  The List ancestor calls them Clear and
  18. Delete, respectively, which I found extremely odd since _Borland_ is the one
  19. calling for a standard set of method identifiers....
  20.  
  21. Version 1.00: released to the public domain on 8 July 1989.
  22.    See above for the reason this was developed.
  23.  
  24. Version 1.01: released to the public domain on 17 August 1989.
  25.    Lavern Ogden discovered an endless loop in the Total() function.  This was
  26. due to my using LinkList.Next instead of LinkList.Next().  I fault Borland for
  27. this error -- there are two "Next" possibilities.  Shame shame!
  28.    Added a new function, Specific(), which returns a NodePtr to the nth node
  29. in the list.  This will be very handy for people who use TurboPower Software's
  30. TPPICK unit from their TPRO5 toolkit.}
  31.  
  32.  
  33. INTERFACE {section}
  34.  
  35.  
  36. USES
  37.    Objects;
  38.  
  39. TYPE
  40.    LinkList
  41.     = OBJECT(List)
  42.       PROCEDURE Init;
  43.       PROCEDURE Done;
  44.  
  45.       PROCEDURE Before(TheNode    : NodePtr;
  46.                        BeforeNode : NodePtr);
  47.       PROCEDURE After (TheNode   : NodePtr;
  48.                        AfterNode : NodePtr);
  49.  
  50.       FUNCTION  Specific(NodePos : LONGINT) : NodePtr;
  51.       FUNCTION  Total(TheNode : NodePtr) : LONGINT;
  52.       END;
  53.  
  54.  
  55. IMPLEMENTATION {section}
  56.  
  57.  
  58. {============================================================================}
  59. PROCEDURE LinkList.Init;
  60.  
  61.    {This procedure initializes the LinkList.}
  62.  
  63. BEGIN {LinkList.Init}
  64. Clear
  65. END; {LinkList.Init}
  66. {============================================================================}
  67.  
  68. {============================================================================}
  69. PROCEDURE LinkList.Done;
  70.  
  71.    {This procedure deletes all nodes from the list.}
  72.  
  73. BEGIN {LinkList.Done}
  74. Delete
  75. END; {LinkList.Done}
  76. {============================================================================}
  77.  
  78. {============================================================================}
  79. PROCEDURE LinkList.Before(TheNode    : NodePtr;
  80.                           BeforeNode : NodePtr);
  81.  
  82.    {Places TheNode in the LinkList just before BeforeNode.}
  83.  
  84. BEGIN {LinkList.Before}
  85. IF (TheNode = NIL)
  86.  THEN
  87.     EXIT {there's nothing to add to our LinkList}
  88.  ELSE
  89.     IF ((BeforeNode = NIL)
  90.      OR (BeforeNode^.Prev = NIL))
  91.      THEN {append as first node on the LinkList}
  92.         Append(TheNode)
  93.      ELSE
  94.         BEGIN
  95.         TheNode^.Next          := BeforeNode;
  96.         BeforeNode^.Prev^.Next := TheNode
  97.         END
  98. END; {LinkList.Before}
  99. {============================================================================}
  100.  
  101. {============================================================================}
  102. PROCEDURE LinkList.After(TheNode   : NodePtr;
  103.                          AfterNode : NodePtr);
  104.  
  105.    {Places TheNode in the LinkList just after AfterNode.}
  106.  
  107. BEGIN {LinkList.After}
  108. IF (TheNode = NIL)
  109.  THEN
  110.     EXIT {there's nothing to add to our LinkList}
  111.  ELSE
  112.     IF ((AfterNode = NIL)
  113.      OR (AfterNode^.Next = NIL))
  114.      THEN {Insert as last node on the LinkList}
  115.         Insert(TheNode)
  116.      ELSE
  117.         BEGIN
  118.         TheNode^.Next   := AfterNode^.Next;
  119.         AfterNode^.Next := TheNode
  120.         END
  121. END; {LinkList.After}
  122. {============================================================================}
  123.  
  124. {============================================================================}
  125. FUNCTION  LinkList.Specific(NodePos : LONGINT) : NodePtr;
  126.  
  127.    {Returns the NodePtr to the nth node in the list, represented by NodePos.
  128. The returned value will be NIL if NodePos <= 0, or if it is > Total.}
  129.  
  130. VAR
  131.    TempNodePtr : NodePtr;
  132.    Index       : LONGINT;
  133.  
  134. BEGIN {LinkList.Specific}
  135. {Make sure we have a valid NodePos value.}
  136. IF ((NodePos <= 0) OR (NodePos > Total(First)))
  137.  THEN
  138.     Specific := NIL
  139.  ELSE
  140.     BEGIN
  141.     TempNodePtr := First;
  142.     Index       := 1;
  143.  
  144.     WHILE (Index < NodePos)
  145.      DO BEGIN
  146.         TempNodePtr := Next(TempNodePtr);
  147.         INC(Index)
  148.         END;
  149.  
  150.     Specific := TempNodePtr
  151.     END
  152. END; {LinkList.Specific}
  153. {============================================================================}
  154.  
  155. {============================================================================}
  156. FUNCTION  LinkList.Total(TheNode : NodePtr) : LONGINT;
  157.  
  158.    {This function returns a number corresponding to the number of nodes on the
  159. LinkList.  It starts counting from TheNode^.  Therefore, to get a total count
  160. of all nodes on the LinkList, use LinkListVar.Total(LinkListVar.First).}
  161.  
  162. VAR
  163.    TempLongint : LONGINT;
  164.  
  165. BEGIN {LinkList.Total}
  166. {Initialize.}
  167. TempLongint := 0;
  168.  
  169. IF (TheNode = NIL)
  170.  THEN
  171.     {Do nothing.}
  172.  ELSE
  173.     REPEAT
  174.         INC(TempLongint);
  175.         TheNode := Next(TheNode)
  176.      UNTIL (TheNode = NIL);
  177.  
  178. Total := TempLongint
  179. END; {LinkList.Total}
  180. {============================================================================}
  181.  
  182.  
  183. END. {ObjectA}
  184.